﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using HubbleDataAccess;
using DreamBuilder.Common;

namespace xHubble.SingleWell.DataAccess
{
    public class ArchiveDAL
    {
        /// <summary>
        /// 查询档案资料
        /// </summary>
        /// <param name="archiveType">档案类型 1文书 2产品 3科研 4建设 5设备 6会计 7油气勘探 8声像 9实物</param>
        /// <param name="keywords"></param>
        /// <param name="pageinfo"></param>
        /// <returns></returns>
        public static DataTable SearchArchiveData(int archiveType, string keywords, PageInfo pageinfo)
        {
            DataTable singlewellTable = null;
            string viewName = string.Empty;
            switch(archiveType)
            {
                case 9:
                    viewName = "v_realkind";
                    break;
                case 2:
                    viewName = "v_cd";
                    break;
                case 3:
                    viewName = "v_technology";
                    break;
                case 4:
                    viewName = "v_buildproject";
                    break;
                case 5:
                    viewName = "v_equipment";
                    break;
                case 6:
                    viewName = "v_account";
                    break;
                case 7:
                    viewName = "v_olisearch";
                    break;
                case 8:
                    viewName = "v_audiovedio";
                    break;
                case 1:
                default:
                    viewName = "v_document";
                    break;
            }
            string matchString = HubbleHelper.GetMatchStringSelf(viewName, "search_val", keywords);
            string searchSql = string.Format("select between {0} to {1} * from {2} where search_val contains '{3}'"
                , pageinfo.PageSize * (pageinfo.PageIndex - 1)
                , pageinfo.PageSize * pageinfo.PageIndex - 1
                ,viewName
                , matchString
                );
            singlewellTable = HubbleDBAccess.ExecuteDataTable(searchSql);
            pageinfo.RecordCount = singlewellTable.MinimumCapacity;
            return singlewellTable;
        }

        /// <summary>
        /// 查询档案资料
        /// </summary>
        /// <param name="archiveType">档案类型 1文书 2产品 3科研 4建设 5设备 6会计 7油气勘探 8声像 9实物</param>
        /// <param name="archiveNum">档号</param>
        /// <returns></returns>
        public static DataTable SearchArchiveData(int archiveType, string archiveNum)
        {
            DataTable singlewellTable = null;
            string viewName = string.Empty;
            switch (archiveType)
            {
                case 9:
                    viewName = "v_realkind";
                    break;
                case 2:
                    viewName = "v_cd";
                    break;
                case 3:
                    viewName = "v_technology";
                    break;
                case 4:
                    viewName = "v_buildproject";
                    break;
                case 5:
                    viewName = "v_equipment";
                    break;
                case 6:
                    viewName = "v_account";
                    break;
                case 7:
                    viewName = "v_olisearch";
                    break;
                case 8:
                    viewName = "v_audiovedio";
                    break;
                case 1:
                default:
                    viewName = "v_document";
                    break;
            }
            string searchSql = string.Format("select top 1 * from {0} where archivenum = '{1}'"
                , viewName
                , archiveNum
                );
            singlewellTable = HubbleDBAccess.ExecuteDataTable(searchSql);
            return singlewellTable;
        }
    }
}
